<?

Smutty_Main::loadClass( 'Smutty_DatabaseResult' );

/**
 *  singleton for the database
 *
 */

class Smutty_Database {

	var $cnn = null;

	function Smutty_Database() {
		$cfg = Smutty_Config::getInstance();
		$this->cnn = mysql_connect(
			$cfg->db->host,
			$cfg->db->username,
			$cfg->db->password
		);
		mysql_select_db( $cfg->db->name, $this->cnn );
	}

	function getInstance() {
		static $instance;
		if ( $instance == null )
			$instance = new Smutty_Database();
		return $instance;
	}

	function query( $sql ) {
		$res = mysql_query( $sql, $this->cnn );
		return $res ? new Smutty_DatabaseResult($res) : false;
	}

	function update( $sql ) {
		return mysql_query($sql,$this->cnn) ? true : false;
	}

	function quote( $string ) {
		return preg_replace( '/\'/', '\\\'', $string );
	}

	function getInsertId() {
		return mysql_insert_id( $this->cnn );
	}

	function getError() {
		return mysql_error( $this->cnn );
	}

	function getCurrentDate() {
		return date( 'Y-m-d h:i:s' );
	}

}

?>